package com.company.app.serviceImpl;

import java.util.Arrays;
import java.util.List;

import javax.jws.WebService;
import javax.xml.ws.WebServiceException;

import com.company.app.bo.CustomerBO;
import com.company.app.entities.Customer;
import com.company.app.service.CustomerService;
import com.company.app.validators.CustomerValidator;

/**
 * The Class CustomerServiceImpl.
 */
@WebService(endpointInterface = "com.company.app.service.CustomerService", name = "CustomerService")
public class CustomerServiceImpl implements CustomerService {

	/** The customer bo. */
	private CustomerBO customerBO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.CustomerService#getCustomerById(java.lang.Long)
	 */
	@Override
	public Customer getCustomerById(Long id) {
		return this.customerBO.getCustomerById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.CustomerService#searchCustomerByFilter(com.company
	 * .app.entities.Customer)
	 */
	@Override
	public List<Customer> searchCustomerByFilter(Customer customer) {
		return this.customerBO.searchCustomers(Arrays.asList(customer));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.CustomerService#executeCustomerBussinessLogic()
	 */
	@Override
	public boolean executeCustomerBussinessLogic() {
		return this.customerBO.executeCustomerBussinessLogic();
	}

	/**
	 * Sets the customer bo.
	 * 
	 * @param customerBO
	 *            the new customer bo
	 */
	public void setCustomerBO(CustomerBO customerBO) {
		this.customerBO = customerBO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.CustomerService#createCustomer(com.company.app
	 * .entities.Customer)
	 */
	@Override
	public boolean createCustomer(Customer customer) throws WebServiceException {
		String erroMsg = CustomerValidator.validCustomer(customer);
		if (erroMsg.length() > 0) {
			throw new WebServiceException(erroMsg);
		}
		return this.customerBO.createCustomer(customer) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.CustomerService#updateCustomer(com.company.app
	 * .entities.Customer)
	 */
	@Override
	public boolean updateCustomer(Customer customer) throws WebServiceException {
		String erroMsg = CustomerValidator.validCustomer(customer);
		if (erroMsg.length() > 0) {
			throw new WebServiceException(erroMsg);
		}
		return this.customerBO.updateCustomer(customer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.company.app.service.CustomerService#deleteCustomer(com.company.app
	 * .entities.Customer)
	 */
	@Override
	public boolean deleteCustomer(Customer customer) {
		return this.customerBO.deleteCustomer(customer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.company.app.service.CustomerService#createTmpCustomer()
	 */
	@Override
	public Customer createTmpCustomer() throws WebServiceException {
		Customer newCustomer = new Customer();
		newCustomer.setName("N/A");
		newCustomer.setEmail("N/A");
		return this.customerBO.createCustomer(newCustomer);
	}

}
